package com.future.mayi.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.baselib.view.LoadingDialog;
import com.future.mayi.R;
import com.future.mayi.activity.BaseActivity;
import com.future.mayi.activity.BaseFragment;
import com.future.mayi.activity.EvaluationActivity;
import com.future.mayi.activity.GoodsOrderActivity;
import com.future.mayi.activity.PayActivity;
import com.future.mayi.adapter.GoodsAllOrderAdapter;
import com.future.mayi.app.App;
import com.future.mayi.dialog.ConfirmGoodsDialog;
import com.future.mayi.model.GoodsOrderModel;
import com.future.mayi.model.interfaces.DialogEnterListener;
import com.future.mayi.model.interfaces.OnItemClickListener;
import com.future.mayi.response.GoodsOrderListResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import butterknife.BindView;

public class GoodsAllOrderFragment extends BaseFragment implements XRecyclerView.LoadingListener, OnItemClickListener {
    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;
    @BindView(R.id.empty_view)
    View emptyView;


    private Integer status;
    private int page = 0;
    private String searchContent;
    private GoodsAllOrderAdapter adapter;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_goods_all_order;
    }

    @Override
    protected void initView() {
        recyclerView.setHasFixedSize(true);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new GoodsAllOrderAdapter(getActivity(), recyclerView);
        recyclerView.setAdapter(adapter);
        recyclerView.setEmptyView(emptyView);
    }

    @Override
    protected void initListener() {
        recyclerView.setLoadingListener(this);
        adapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        if (status != null)
            recyclerView.refresh();
    }

    //"全部", "待付款", "待发货", "待收货", "待评价", "退款审核中"
    //订单状态:0=待支付,10=已支付,20=已发货,30=已确认,待评价,50=已完成,60=已取消,70=退款中,71=退款失败,80=退款成功 (不传代表获取全部订单
    public void setStyle(int style) {
        switch (style) {
            case 0:
                status = -1;
                break;
            case 1:
                status = 0;
                break;
            case 2:
                status = 10;
                break;
            case 3:
                status = 20;
                break;
            case 4:
                status = 70;
                break;
        }
    }

    @Override
    public void onRefresh() {
        page = 0;
        getData();

    }

    @Override
    public void onLoadMore() {
        page++;
        getData();
    }


    private void getData() {
        new HttpRequest<GoodsOrderListResponse>()
                .with(getActivity()).setApiCode(ApiCst.getOrderList)
                .addParam("token", App.getToken(mContext))
                .addParam("status", (status == null || status == -1) ? null : status)
                .addParam("p", page)
                .addParam("psize", 10)
                .addParam("keywords", searchContent)
                .setListener(new HttpRequest.OnNetworkListener<GoodsOrderListResponse>() {
                    @Override
                    public void onSuccess(GoodsOrderListResponse response) {
                        HCUtils.refreshListForPage(recyclerView, adapter, response.list, page, 10);
                    }

                    @Override
                    public void onFail(String message, GoodsOrderListResponse response) {
                        page = HCUtils.refreshFail(recyclerView, page, getContext(), message);
                    }
                }).start(new GoodsOrderListResponse());
    }


    @Override
    public void OnItem(View view, int position) {
        GoodsOrderModel model = adapter.getItem(position);
        if (model == null) return;
        if (view.getId() == R.id.tv_enter) {
            switch (model.getStatus()) {
                case 0:
                    PayActivity.startActivity(getActivity(), String.valueOf(model.getOrder_id()), model.getPay_price());
                    break;
                case 20:
                    //确认收货
                    confirm(position);
                    break;
                default:
                    break;
            }
            return;
        }
        GoodsOrderActivity.startActivity(getActivity(), model);
    }

    @Override
    public void OnLongItem(View view, int position) {

    }

    /**
     * 确认收货
     */
    private void confirm(final int position) {
        ConfirmGoodsDialog confirmGoodsDialog = new ConfirmGoodsDialog(getActivity());
        confirmGoodsDialog.setListener(new DialogEnterListener() {
            @Override
            public void cancel(Object tag) {

            }

            @Override
            public void enter(Object tag) {
                confirmOrCancel(30, position);
            }
        });
        confirmGoodsDialog.show();
    }

    /**
     * 确认收货或取消订单
     * 30-确认收货 60-取消订单
     *
     * @param status
     */
    public void confirmOrCancel(int status, final int position) {
        ((BaseActivity) getActivity()).showLoadingDialog();
        new HttpRequest<DefaultResponse>()
                .with(getActivity()).setApiCode(ApiCst.confirmOrCancel)
                .addParam("token", App.getToken(mContext))
                .addParam("order_id", adapter.getItem(position).getOrder_id())
                .addParam("status", status)
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                        ((BaseActivity) getActivity()).dissmissLoadingDialog();
                        toast(response.message);
                        adapter.getItem(position).setStatus(30);
                        recyclerView.notifyItemChanged(position);
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {
                        ((BaseActivity) getActivity()).dissmissLoadingDialog();
                        toast(message);
                    }
                }).start(new DefaultResponse());

    }


    public void refresh() {
        if (recyclerView != null && adapter != null)
            recyclerView.refresh();
    }

    public void setSearchContent(String searchContent) {
        if (TextUtils.equals(this.searchContent, searchContent)) return;
        this.searchContent = searchContent;
        if (recyclerView != null)
            recyclerView.refresh();
    }
}
